//
//  SettingLaunch.swift
//  Youtube2
//
//  Created by 劢克科技 on 2019/6/12.
//  Copyright © 2019 meckey. All rights reserved.
//

import UIKit

class SettingModel: NSObject {
    var title = ""
    var icon = UIImageView()
    
    init(title: String, icon: UIImageView) {
        self.title = title
        self.icon = icon
    }
}

protocol SettingLaunchDelegate {
    func handleDismiss(setting: String)
}

class SettingLaunch: NSObject {
    
    var datas = [SettingModel]()
    let cellHeight: CGFloat = 50
    
    override init() {
        super.init()
        
        datas.append(SettingModel(title: "Settings", icon: UIImageView(image: #imageLiteral(resourceName: "settings"))))
        datas.append(SettingModel(title: "Terms & privacy policy", icon: UIImageView(image: #imageLiteral(resourceName: "privacy"))))
        datas.append(SettingModel(title: "Send Feedback", icon: UIImageView(image: #imageLiteral(resourceName: "feedback"))))
        datas.append(SettingModel(title: "Help", icon: UIImageView(image: #imageLiteral(resourceName: "help"))))
        datas.append(SettingModel(title: "Switch Account", icon: UIImageView(image: #imageLiteral(resourceName: "switch_account"))))
        datas.append(SettingModel(title: "Cancel", icon: UIImageView(image: #imageLiteral(resourceName: "cancel"))))
    }
    
    let cellId = "cellId"
    var delegate: SettingLaunchDelegate?
    
    lazy var cover: UIView = {
        let view = UIView()
        view.backgroundColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0.5)
        view.isUserInteractionEnabled = true
        let gesture = UITapGestureRecognizer(target: self, action: #selector(hideLaunch))
        view.addGestureRecognizer(gesture)
        return view
    }()
    
    lazy var collectionView: UICollectionView = {
        let view = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
        view.backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
        view.delegate = self
        view.dataSource = self
        view.register(SettingCell.self, forCellWithReuseIdentifier: self.cellId)
        return view
    }()
    
    @objc func hideLaunch() {
        let collectionHeight = CGFloat(datas.count) * (cellHeight + 0)
        UIView.animate(withDuration: 0.5, animations: {
            self.cover.alpha = 0
            self.collectionView.frame = CGRect(x: 0, y: UIScreen.main.bounds.height, width: UIScreen.main.bounds.width, height: collectionHeight)
        }) { (_) in
            self.cover.removeFromSuperview()
            self.collectionView.removeFromSuperview()
        }
    }
    
    func showLaunch() {
        guard let window = UIApplication.shared.keyWindow else { return }
        
        window.addSubview(cover)
        cover.frame = window.bounds
        
        cover.alpha = 0
        UIView.animate(withDuration: 0.5) {
            self.cover.alpha = 1
        }
        
        window.addSubview(collectionView)
        let collectionHeight = CGFloat(datas.count) * (cellHeight + 0)
        collectionView.frame = CGRect(x: 0, y: UIScreen.main.bounds.height, width: UIScreen.main.bounds.width, height: collectionHeight)
        
        UIView.animate(withDuration: 0.5, animations: {
            let y = UIScreen.main.bounds.height - collectionHeight
            self.collectionView.frame = CGRect(x: 0, y: y, width: UIScreen.main.bounds.width, height: collectionHeight)
        })
    }
}

extension SettingLaunch: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return datas.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! SettingCell
        cell.title.text = datas[indexPath.row].title
        cell.icon.image = datas[indexPath.row].icon.image
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: collectionView.frame.width, height: cellHeight)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let collectionHeight = CGFloat(datas.count) * (cellHeight + 0)
        UIView.animate(withDuration: 0.5, animations: {
            self.cover.alpha = 0
            self.collectionView.frame = CGRect(x: 0, y: UIScreen.main.bounds.height, width: UIScreen.main.bounds.width, height: collectionHeight)
        }) { (_) in
            self.cover.removeFromSuperview()
            self.collectionView.removeFromSuperview()
            let setting = self.datas[indexPath.item].title
            
            if setting != "Cancel" {
                self.delegate?.handleDismiss(setting: setting)
            }
        }
    }
}
